package com.rockcent.wechat.service;

import com.rockcent.helper.MallFundHelper;
import com.rockcent.mall.lib.domain.*;
import com.rockcent.mall.lib.domain.enumclass.*;
import com.rockcent.mall.lib.helper.CustomerFundFlowHelper;
import com.rockcent.mall.lib.helper.MallFundFlowHelper;
import com.rockcent.mall.lib.helper.OrderHelper;
import com.rockcent.mall.lib.helper.OrderStatusHelper;
import com.rockcent.mall.lib.repository.*;
import com.rockcent.wechat.helper.MallOrderPaymentHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by DK on 17/2/20.
 */
@Service
public class WechatTestPaymentService {
    @Autowired
    private OrderCommitService orderCommitService;
    @Autowired
    private MallOrderRepository mallOrderRepository;
    @Autowired
    private MallOrderPaymentHelper mallOrderPaymentHelper;
    @Autowired
    private CustomerRepository customerRepository;
    @Autowired
    private CustomerFundRepository customerFundRepository;
    @Autowired
    private MallOrderPaymentRepository mallOrderPaymentRepository;
    @Autowired
    private MallOrderPaymentFlowRepository mallOrderPaymentFlowRepository;
    @Autowired
    private com.rockcent.service.PaymentService paymentService;
    @Autowired
    private MerchantOrderRepository merchantOrderRepository;
    @Autowired
    private MallFundRepository mallFundRepository;
    @Autowired
    private CustomerFundFlowRepository customerFundFlowRepository;
    @Autowired
    private MallFundFlowRepository mallFundFlowRepository;
    @Autowired
    private MallCashCouponFlowRepository mallCashCouponFlowRepository;


    @Transactional
    public void testPayment(String orderNum, Long customerId){
        Customer customer = customerRepository.findOne(customerId);

        MallOrder mallOrder = mallOrderRepository.findByOrderNum(orderNum);

        // 用户资金
        CustomerFund customerFund = customerFundRepository.findOne(customerId);


        // 创建平台订单支付
        MallOrderPayment mallOrderPayment = mallOrderPaymentHelper.create(OrderHelper.SINGLEONE.genMallOrderPaymentNumber(), mallOrder.getActualAmount(), mallOrder.getMallId(), mallOrder.getId(), mallOrder.getOrderNum(), customerId, MallOrderPaymentStatus.SUCCESS);
        mallOrderPayment = mallOrderPaymentRepository.save(mallOrderPayment);

        // 添加支付流水
        MallOrderPaymentFlow mallOrderPaymentFlow = new MallOrderPaymentFlow();
        mallOrderPaymentFlow.setFlowNum(OrderHelper.SINGLEONE.genMallOrderPaymentFlowNumber());
        mallOrderPaymentFlow.setPaymentNum(mallOrderPayment.getPaymentNum());
        mallOrderPaymentFlow.setCustomerId(22L);
        mallOrderPaymentFlow.setMallOrderId(mallOrder.getId());
        mallOrderPaymentFlow.setMallOrderNum(mallOrder.getOrderNum());
        mallOrderPaymentFlow.setMallId(mallOrder.getMallId());
        mallOrderPaymentFlow.setAmount(mallOrder.getActualAmount());
        mallOrderPaymentFlow.setPaymentMethod(PaymentMethod.BALANCE);
        mallOrderPaymentFlow.setStatus(MallOrderPaymentStatus.SUCCESS); // 支付成功
        mallOrderPaymentFlowRepository.save(mallOrderPaymentFlow);

        OrderStatusHelper.SINGLETON.mallOrderPayed(mallOrder); //修改平台订单为已支付
        mallOrder.setPaymentMethod(PaymentMethod.BALANCE);
        mallOrderRepository.save(mallOrder);

        List<MerchantOrder> merchantOrderList = merchantOrderRepository.findByMallOrderNum(mallOrder.getOrderNum());
        // 支付成功 修改商家订单
        paymentService.modifyMerchantOrder(mallOrder,merchantOrderList);

        // 用户余额支付
        CustomerFundFlow expendCustomerFundFlow = CustomerFundFlowHelper.SINGLEONE.create(customerFund, mallOrder, mallOrder.getActualAmount(), BigDecimal.ZERO, mallOrder.getActualAmount(), FundTransactionFlow.EXPEND, CustomerFundFlowTransactionType.BALANCE_EXPEND, CustomerFundFlowTransactionType.BALANCE_EXPEND.getValue(), PaymentMethod.BALANCE);
        customerFund.setAvailableAmount(expendCustomerFundFlow.getAvailableAmount());
        customerFund.setFrozen(expendCustomerFundFlow.getFrozen());
        customerFund.setTotalAmount(expendCustomerFundFlow.getAvailableAmount().add(expendCustomerFundFlow.getFrozen()));
        customerFundFlowRepository.save(expendCustomerFundFlow);
        // 平台资金流
        MallFund mallFund = mallFundRepository.findOne(mallOrder.getMallFundId());

        // 平台冻结资金加 用户余额支付
        MallFundFlow balanceMallFundFlow = MallFundFlowHelper.SINGLEONE.create(mallFund, mallOrder.getOrderNum(), BigDecimal.ZERO, mallOrder.getActualAmount(), mallOrder.getActualAmount(), FundTransactionFlow.INCOME, MallFundFlowTransactionType.F_BALANCE_SUCCESS_FROZEN, PaymentMethod.BALANCE, null);
        balanceMallFundFlow = mallFundFlowRepository.save(balanceMallFundFlow);
        balanceMallFundFlow.setParentId(balanceMallFundFlow.getId());
        MallFundHelper.SINGLEONE.calculationMallFundAmount(mallFund, balanceMallFundFlow.getFrozen(), balanceMallFundFlow.getAvailableAmount());


        // 使用现金券
        if (mallOrder.getCashCouponId() != null) {
            // 现金券余额减 流水
            MallCashCouponFlow mallCashCouponFlow = new MallCashCouponFlow();
            mallCashCouponFlow.setCashCouponId(mallOrder.getCashCouponId());
            mallCashCouponFlow.setMallId(mallOrder.getMallId());
            mallCashCouponFlow.setTransactionFlow(FundTransactionFlow.EXPEND);
            mallCashCouponFlow.setOperationAmount(mallOrder.getCashCouponAmount());
            mallCashCouponFlowRepository.save(mallCashCouponFlow);
            // 平台现金券减
            mallFund.setAvailableCashAmount(mallFund.getAvailableCashAmount().subtract(mallOrder.getCashCouponAmount()));

            // 平台冻结资金加 现金券支付
            MallFundFlow cashCouponMallFundFlow = MallFundFlowHelper.SINGLEONE.create(mallFund, mallOrder.getOrderNum(), BigDecimal.ZERO, mallOrder.getCashCouponAmount().subtract(mallOrder.getCashCouponSurplusAmount()), mallOrder.getCashCouponAmount().subtract(mallOrder.getCashCouponSurplusAmount()), FundTransactionFlow.INCOME, MallFundFlowTransactionType.F_CASH_COUPON_SUCCESS_FROZEN, PaymentMethod.CASH_COUPON, null, balanceMallFundFlow.getParentId());
            cashCouponMallFundFlow = mallFundFlowRepository.save(cashCouponMallFundFlow);
            MallFundHelper.SINGLEONE.calculationMallFundAmount(mallFund, cashCouponMallFundFlow.getFrozen(), cashCouponMallFundFlow.getAvailableAmount());
            // 订单 现金券剩余金额 > 0
            if (mallOrder.getCashCouponSurplusAmount().compareTo(BigDecimal.ZERO) == 1) {
                // 平台冻结资金加 现金券多出部分
                MallFundFlow surplusCashCouponMallFundFlow = MallFundFlowHelper.SINGLEONE.create(mallFund, mallOrder.getOrderNum(), BigDecimal.ZERO, mallOrder.getCashCouponSurplusAmount(), mallOrder.getCashCouponSurplusAmount(), FundTransactionFlow.INCOME, MallFundFlowTransactionType.F_CASH_COUPON_SURPLUS_SUCCESS_FROZEN_INCOME, PaymentMethod.CASH_COUPON, null, balanceMallFundFlow.getParentId());
                surplusCashCouponMallFundFlow = mallFundFlowRepository.save(surplusCashCouponMallFundFlow);
                MallFundHelper.SINGLEONE.calculationMallFundAmount(mallFund, surplusCashCouponMallFundFlow.getFrozen(), surplusCashCouponMallFundFlow.getAvailableAmount());
            }
        }

        // 平台资金账户
        mallFundRepository.save(mallFund);

        // 当服务订单内包含不可退产品支付成功,马上结算
        paymentService.productServiceSettled(mallOrder, merchantOrderList);

        // 如果购买礼包赠送卡券时，支付成功把卡券赠送到用户
        paymentService.giftGiveCoupon(mallOrder);
    }
}
